package com.vids.utils;

import com.alibaba.fastjson2.JSON;
import com.vids.mapper.ApeMapper;
import com.vids.model.entity.Ape;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Component
public class AlarmUtils {

    @Resource
    public ApeMapper apeMapper;

//    public boolean isInited = false;
//
    public boolean isLoaded = false;

    @PostConstruct
    public HashMap<String, Ape>  getApeObjectMap()
    {
        HashMap<String, Ape> apeMap = new HashMap<>();
        try {
            List<Ape> all = apeMapper.findAll();
            if (all.isEmpty()) {
                log.info("apeMapper.findAll() failed, record is empty.");
            }

            for (Ape ape : all) {
                apeMap.put(ape.getApeID(), ape);
            }

            return apeMap;
        }
        catch (Exception e)
        {
            log.info("===========> getApeObjectMap failed.", e);
            return null;
        }
    }

    /**
     * 查询设备信息表，创建并初始化map  用来计数
     */
    @PostConstruct
    public HashMap<String, String>  getApeMapJson() throws IOException {
        // 查询出设备信息
        HashMap<String, String> apeMap = new HashMap<>();
        HashMap<String, String> apeId2IpAddrMap = new HashMap<>();
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        FileOutputStream fos2 = null;
        BufferedOutputStream bos2 = null;
        try {
            List<Ape> all = apeMapper.findAll();
            if (all.isEmpty())
            {
                log.info("apeMapper.findAll() failed, record is empty.");
            }

            for (Ape ape:all) {
                apeMap.put(ape.getIPAddr(), ape.getApeID());

                apeId2IpAddrMap.put(ape.getApeID(), ape.getIPAddr());
            }

            fos = new FileOutputStream("./apeMap.json");
            // 创建一个BufferedOutputStream对象，并指定缓冲区大小为1024字节
            bos = new BufferedOutputStream(fos, 1024);
            JSON.writeTo(bos, apeMap);
            bos.flush();
            bos.close();
            fos.close();

            fos2 = new FileOutputStream("./apeId2IpAddrMap.json");
            bos2 = new BufferedOutputStream(fos2, 1024);
            JSON.writeTo(bos2, apeId2IpAddrMap);
            bos2.flush();
            bos2.close();
            fos2.close();

            return apeMap;

        } catch (IOException ioe) {
            log.info("===========> Save apeMap.json io failed.", ioe);
            ioe.printStackTrace();
        }
        catch (Exception e)
        {
            log.info("===========> Save apeMap.json failed.", e);
            e.printStackTrace();
        } finally {
            bos.close();
            fos.close();

            bos2.close();
            fos2.close();

            bos = null;
            fos = null;

            bos2 = null;
            fos2 = null;
        }

        return apeMap;
    }

    public static HashMap loadApeId2IpAddrMapJson()
    {
        try {
            HashMap apeMap = new HashMap<>();
            String content = new String(Files.readAllBytes(Paths.get("./apeId2IpAddrMap.json")));

            apeMap = JSON.to(apeMap.getClass(), content);

            return apeMap;

        } catch (IOException ioe) {
            log.info("===========> Save apeId2IpAddrMap.json io failed.", ioe);
            return null;
        }
        catch (Exception e)
        {
            log.info("===========> Save apeId2IpAddrMap.json failed.", e);
            return null;
        }
    }
    public static HashMap loadApeMapJson()
    {
        try {
            HashMap apeMap = new HashMap<>();
            String content = new String(Files.readAllBytes(Paths.get("./apeMap.json")));

//            log.info("content"+content);
            apeMap = JSON.to(apeMap.getClass(), content);

            return apeMap;

        } catch (IOException ioe) {
            log.info("===========> Save apeMap.json io failed.", ioe);
            return null;
        }
        catch (Exception e)
        {
            log.info("===========> Save apeMap.json failed.", e);
            return null;
        }
    }

//    public static void main(String[] args) {
//        AlarmUtils.loadApeMapJson();
//    }

    /**
     * 获得指定日期的后一天
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+1);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        return dayAfter;
    }
    /**
     * 获得指定日期的前一天
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay){
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day-1);

        String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayBefore;
    }
    public static  Date convert(String s) {
        Date date = null;
        //实现将字符串转成⽇期类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


}
